CC=gcc
CFLAGS+=-Wall
OS=$(shell uname | tr 'A-Z' 'a-z')
CPU=$(shell uname -m | tr 'A-Z' 'a-z')
LIBPATH=../../../common/c
INCLS=-I$(LIBPATH)/include
LIBS_DEBUG=-L$(LIBPATH)/lib -lsoar_g
LIBS=-L$(LIBPATH)/lib -lsoar
ifeq ($(OS), linux)
    ifeq ($(CPU), x86_64)
        LIBS_DEBUG+=-L/usr/lib64
        LIBS+=-L/usr/lib64
    else
        LIBS_DEBUG+=-L/usr/lib
        LIBS+=-L/usr/lib
    endif
endif
ifeq ($(OS), freebsd)
    INCLS+=-I/usr/local/include
    LIBS_DEBUG+=-L/usr/local/lib
    LIBS+=-L/usr/local/lib
endif
LIBS_DEBUG+=-lgd -lcrypto -lpthread
LIBS+=-lgd -lcrypto -lpthread

OBJS=main.o monitor.o listener.o worker.o generator.o svc/svc.o svc/svc_hello.o svc/svc_confirm.o
SERVER=$(shell awk '/SERVER *=/ { split($$0,arr,"="); gsub(" ","",arr[2]); print arr[2]; }' ../server.cnfg)
SERVER_DEBUG=$(SERVER)_debug

.PHONY : default debug release clean

default : debug

all : debug release

debug : $(SERVER_DEBUG)

$(SERVER_DEBUG) : $(OBJS)
	$(CC) $(CFLAGS) $(INCLS) -o $@ $^ $(LIBS_DEBUG)
	chmod 755 $@

$(OBJS) : %.o : %.c
	$(CC) $(CFLAGS) -D_MY_DEBUG -g $(INCLS) -c -o $@ $<

release : $(SERVER)

$(SERVER) : $(OBJS:%.o=%.c)
#$(CC) $(CFLAGS) -O3 $(INCLS) -o $@ $^ $(LIBS)
	$(CC) $(CFLAGS) -g $(INCLS) -o $@ $^ $(LIBS_DEBUG)
	chmod 755 $@

clean :
	rm -f core* $(OBJS) $(SERVER) $(SERVER_DEBUG)
